<?php
if (!defined('IN_ZWS')) exit();

class ContentAction extends PublicAction {
	function index() {
		header('Location:' . U('newArticle'));
	} 

	function books($res = 0, $tpl = '') {
		$article = D('ArticleView');
		$slt = $this -> _get('slt');
		if (!empty($slt) && in_array($slt, array('cid', 'gpid', 'gdid', 'sid', 'uid'))) {
			$sql = array($slt => $this -> _get($slt), 'Article.res' => $res);
			$this -> assign('slt', $slt);
		} else $sql = 'Article.res=' . $res;
		$count = $article -> where($sql) -> count();
		import('ORG.Util.Page');
		$page = new Page($count, 10);
		$show = $page -> show();
		$articles = $article -> where($sql) -> limit($page -> firstRow . ',' . $page -> listRows) -> select();
		$this -> assign('articles', $articles);
		$this -> assign('count', $count);
		$this -> assign('page', $show);
		$this -> display($tpl);
	} 

	function videos() {
		$this -> books(1, 'videos');
	} 

	function exams() {
		$this -> books(2, 'exams');
	} 

	function comments() {
		$comment = D('CommentView');
		$slt = $this -> _get('slt');
		if (!empty($slt) && in_array($slt, array('res', 'aid', 'pid', 'uid'))) {
			$sql = array($slt => $this -> _get($slt));
			$this -> assign('slt', $slt);
		} else $sql = '1=1';
		$count = $comment -> where($sql) -> count();
		import('ORG.Util.Page');
		$page = new Page($count, 10);
		$show = $page -> show();
		$comments = $comment -> where($sql) -> limit($page -> firstRow . ',' . $page -> listRows) -> select();
		$this -> assign('comments', $comments);
		$this -> assign('count', $count);
		$this -> assign('page', $show);
		$this -> display();
	} 

	function moods() {
		$mood = D('MoodView');
		$slt = $this -> _get('slt');
		if (!empty($slt) && in_array($slt, array('pid', 'uid'))) {
			$sql = array($slt => $this -> _get($slt));
			$this -> assign('slt', $slt);
		} else $sql = '1=1';
		$count = $mood -> where($sql) -> count();
		import('ORG.Util.Page');
		$page = new Page($count, 10);
		$show = $page -> show();
		$moods = $mood -> where($sql) -> limit($page -> firstRow . ',' . $page -> listRows) -> select();
		$this -> assign('moods', $moods);
		$this -> assign('count', $count);
		$this -> assign('page', $show);
		$this -> display();
	} 

	function newArticle() {
		$M = M('GroupGroup');
		$groups = $M -> select();
		$this -> assign('groups', $groups);
		$subjects = $M -> table('zws_group_subject') -> select();
		$this -> assign('subjects', $subjects);
		$this -> display();
	} 

	function edit() {
		$id = intval($this -> _get('id'));
		$article = D('ArticleView');
		$articleInfo = $article -> where('Article.id=' . $id) -> find();
		if (isEmpty($articleInfo)) $this -> error('没有找到该内容！');
		$cate = M('GroupCategory');
		$cates = $cate -> where('res=' . $articleInfo['res']) -> select();
		$group = M('GroupGroup');
		$groups = $group -> select();
		$subject = M('GroupSubject');
		$subjects = $subject -> select();
		$grade = M('GroupGrade');
		$grades = $grade -> where('gid=' . $articleInfo['gpid']) -> select();
		$this -> assign($articleInfo);
		$this -> assign(array('cates' => $cates, 'groups' => $groups, 'subjects' => $subjects, 'grades' => $grades));
		$this -> display();
	} 

	function editComment() {
		$id = intval($this -> _get('id'));
		$comment = D('CommentView');
		$commentInfo = $comment -> where('ArticleComment.id=' . $id) -> find();
		if (isEmpty($commentInfo)) $this -> error('没有找到该评论！');
		$this -> assign($commentInfo);
		$this -> display();
	} 

	function editMood() {
		$id = intval($this -> _get('id'));
		$mood = D('MoodView');
		$moodInfo = $mood -> where('ZoneMood.id=' . $id) -> find();
		if (isEmpty($moodInfo)) $this -> error('没有找到该评论！');
		$this -> assign($moodInfo);
		$this -> display();
	} 

	function doEdit() {
		$id = intval($this -> _get('id'));
		if (isEmpty($id)) $this -> error('非法操作！');
		switch ($this -> _get('type')) {
			case 1:
				$article = D('AddArticle');
				if ($article -> create()) {
					if ($article -> where('id=' . $id) -> save()) $this -> ajaxReturn('', '编辑成功', 0);
					else $this -> ajaxReturn('', $article -> getError(), 1);
				} else {
					$this -> ajaxReturn('', $article -> getError(), 1);
				} 
			case 2:
				$comment = M('ArticleComment');
				if ($comment -> where('id=' . $id) -> save(array('comment' => $this -> _post('comment'))) !== false) $this -> ajaxReturn(array('redirect' => U('comments')), '编辑成功，2秒钟后返回评论列表', 0);
				$this -> ajaxReturn('', '编辑失败！', 1);
			case 3:
				$mood = M('ZoneMood');
				if ($mood -> where('id=' . $id) -> save(array('mood' => $this -> _post('mood'))) !== false) $this -> ajaxReturn(array('redirect' => U('moods')), '编辑成功，2秒钟后返回状态列表', 0);
				$this -> ajaxReturn('', '编辑失败！', 1);
			default:
				$this -> ajaxReturn('', '非法操作', 1);
		} 
	} 

	function addArticle() {
		$article = D('AddArticle');
		if ($article -> create()) {
			if ($article -> add()) $this -> ajaxReturn('', '添加成功', 0);
			else $this -> ajaxReturn('', $article -> getError(), 1);
		} else {
			$this -> ajaxReturn('', $article -> getError(), 1);
		} 
	} 

	function delArticle() {
		$id = $this -> _get('id');
		if (isEmpty($id)) $this -> ajaxReturn('', '非法操作！', 1);
		$article = M('Article');
		if ($article -> delete($id)) {
			$fav = M('ArticleFav');
			$fav -> where('aid=' . $id) -> delete();
			$view = M('ArticleView');
			$view -> where('aid=' . $id) -> delete(); 
			// 由于InnoDB外键关联，删除评论对应的赞信息也将删除
			$comment = M('ArticleComment');
			$comment -> where('aid=' . $id) -> delete();
			$this -> ajaxReturn('', '删除成功！', 0);
		} else $this -> ajaxReturn('', '删除失败！' , 1);
	} 

	function delComment() {
		$id = $this -> _get('id');
		if (isEmpty($id)) $this -> ajaxReturn('', '非法操作！', 1);
		switch ($this -> _get('type')) {
			case 1:
				$M = M('ArticleComment');
				break;
			case 2:
				$M = M('ZoneMood');
				break;
			default:
				$this -> ajaxReturn('', '非法操作！', 1);
		} 
		if ($M -> delete($id)) $this -> ajaxReturn('', '删除成功！', 0);
		else $this -> ajaxReturn('', '删除失败！' , 1);
	} 
} 
